package com.slorgs.web.controller

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.slorgs.business.service.ApiService
import com.slorgs.infrastructure.dto.ApiEnvDTO
import com.slorgs.infrastructure.dto.ApiGroupDTO
import com.slorgs.infrastructure.dto.LinkLoggerDTO
import com.slorgs.infrastructure.dto.ResponseDTO
import com.slorgs.infrastructure.model.Api
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.io.IOException
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest


@RestController
@RequestMapping("/api")
@CrossOrigin
class ApiController {

    @Resource
    var apiService: ApiService? = null

    @PostMapping("/list")
    fun list(@RequestBody api: Api?, page: Page<*>?): ResponseDTO? {
        return apiService?.selectList(page, api)
    }

    @GetMapping("/getApi")
    fun apiList(api: ApiGroupDTO?, page: Page<*>?, request: HttpServletRequest?): ResponseDTO? {
        return apiService?.apiList(api, page, request)
    }

    @GetMapping("/getGroup")
    fun getGroup(): ResponseDTO? {
        return apiService?.groupList()
    }

    @PostMapping("/add/delete")
    fun addOrDelete(@RequestBody api: Api?): ResponseDTO? {
        return apiService?.addOrDelete(api)
    }

    @PostMapping("/addOrDelete")
    fun addApi(@RequestBody api: com.gateway.common.model.Api?): ResponseDTO? {
        return apiService?.addApi(api)
    }

    @GetMapping("/refresh/cache")
    fun refreshCache(id: String?): ResponseDTO? {
        return apiService?.refreshCache(id)
    }

    @GetMapping("/delete")
    fun delete(id: String?): ResponseDTO? {
        return apiService?.deleteApi(id)
    }

    @PostMapping("/open/api/pcmToPcma")
    fun pcmToPcmA(@RequestParam("file") file: MultipartFile?, isSync: String?, backAddress: String?): ResponseDTO? {
        return null
    }

    @PostMapping(value = ["/pic/zip"], consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    fun picZip(@RequestParam("file") file: MultipartFile?): ResponseDTO? {
        return null
    }

    @GetMapping("/env/list")
    fun envList(): ResponseDTO? {
        return apiService?.envList()
    }

    @GetMapping("/audit/log")
    fun auditLog(
        page: Page<*>?,
        idApi: String?,
        @RequestParam(required = false) status: Int?,
        name: String?
    ): ResponseDTO? {
        val linkLoggerDTO = LinkLoggerDTO()
        linkLoggerDTO.setName(name)
        linkLoggerDTO.setIdApi(idApi)
        //        linkLoggerDTO.setStatus(status);
        return apiService?.auditLog(page, linkLoggerDTO)
    }

    @GetMapping("/save/log")
    @Throws(IOException::class)
    fun saveLog(): ResponseDTO? {
        return apiService?.saveLog()
    }

    @GetMapping("/checkApiPath")
    fun checkApiPath(path: String?): ResponseDTO? {
        return apiService?.checkApiPath(path)
    }

    @PostMapping("/online/env")
    fun onlineEnv(@RequestBody apiEnvDTO: ApiEnvDTO?): ResponseDTO? {
        return apiService?.onlineEnv(apiEnvDTO)
    }

    @GetMapping("/statistics")
    fun statistics(): ResponseDTO? {
        return apiService?.statistics()
    }
}